package com.cet.base.collection;

import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * @program: cet-practice
 * @description: 集合测试
 * @author: 陈恩涛
 * @create: 2023-05-06 15:34
 **/
public class CollectionTest {

    public static void main(String[] args) throws InterruptedException {

        ArrayList<TestUser> testUsers = new ArrayList<>();

        TestUser u1 = new TestUser();
        u1.setId("1");
        u1.setName("张三");
        u1.setPinyin("Zhang San");
        u1.setScore(100D);
        testUsers.add(u1);

        TestUser u2 = new TestUser();
        u2.setId("2");
        u2.setName("李四");
        u2.setPinyin("Li Si");
        u2.setScore(100D);
        testUsers.add(u2);

        TestUser u3 = new TestUser();
        u3.setId("3");
        u3.setName("王五");
        u3.setPinyin("Wang Wu");
        u3.setScore(99D);
        testUsers.add(u3);

        u2.setId("3");
        testUsers.add(u2);


        int cupCores = Runtime.getRuntime().availableProcessors();

        ThreadPoolExecutor executor = new ThreadPoolExecutor(
            cupCores + 1,
            cupCores + 1,
            1,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(10 * cupCores),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.CallerRunsPolicy()
        );


        CountDownLatch countDownLatch = new CountDownLatch(testUsers.size());

        for (TestUser testUser : testUsers) {
            executor.execute(() -> {
                System.out.println(Thread.currentThread().getName() + ": "+testUser.getId());
                countDownLatch.countDown();
            });
        }

        countDownLatch.await();

        System.out.println(Thread.currentThread().getName() + "结束！");
        executor.shutdown();
    }
}
